// eslint-disable-next-line camelcase
export const getTableData = axios_get => {
  return {
    data() {
      return {
        data: [],
        dialogSearch: {
          title: "搜索",
          show: false
        },
        but_loading: false,
        pagination: {
          currentPage: 1,
          pageSize: 20
        }
      };
    },
    created() {
      this.init();
    },
    methods: {
      async init() {
        const { pageSize, currentPage } = this.pagination;
        const filter = this.formSearch ? this.formSearch.data : {};
        const params = {
          pageSize,
          currentPage,
          ...filter
        };
        await axios_get(params)
          .then(res => {
            this.data = res.list || res;
            this.pagination.total = res.total;
          })
          .catch(() => {});
      },
      handleSizeChange(pageSize) {
        this.pagination.pageSize = pageSize;
        this.pagination.currentPage = 1;
        this.init();
      },
      currentChange(val) {
        this.pagination.currentPage = val;
        this.init();
      },
      async search() {
        this.dialogSearch.show = false;
        await this.init();
      },
      resetForm() {
        Object.keys(this.formSearch.data).forEach(
          key => (this.formSearch.data[key] = "")
        );
      }
    }
  };
};

// eslint-disable-next-line camelcase
export const deleteTableData = axios_delete => {
  return {
    data() {
      return {
        deleteId: ""
      };
    },
    methods: {
      handleDelete(id) {
        axios_delete({ id }).then(() => {
          this.getTableData();
        });
      },
      handleDeleteList() {
        if (this.deleteId) {
          this.$confirm("정말 삭제 하시겠습니까?", "", {
            confirmButtonText: this.$t("determineBut"),
            cancelButtonText: this.$t("cancelBut"),
            type: "warning"
          })
            .then(() => {
              axios_delete({ id: this.deleteId }).then(() => {
                this.getTableData();
              });
            })
            .catch(() => {});
        }
      },
      emitSelection(item) {
        const arr = item.map(value => {
          return value.id;
        });
        this.deleteId = arr.join(",");
      }
    }
  };
};
